import { defineStore } from 'pinia'

export const useUserInfoStore = defineStore('userInfo', {
    state: () => ({
        userInfo: (() => {
            try {
                const stored = localStorage.getItem('userInfo')
                return stored ? JSON.parse(stored) : null
            } catch {
                return null
            }
        })()
    }),
    actions: {
        setUserInfo(userInfo) {
            this.userInfo = userInfo
            localStorage.setItem('userInfo', userInfo ? JSON.stringify(userInfo) : '')
        },
        // 新增: 退出登录方法
        clearUserInfo() {
            this.userInfo = null
            localStorage.removeItem('userInfo')
        }
    }
})